package com.rrx.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.mapper.OrderMapper;
import com.rrx.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/15.
 */
@Service
public class OrederService {
    @Autowired
    OrderMapper mapper;
    public boolean addOrder(Order order){
        int status=0;
        status=mapper.addOrder(order);
        if(status>0){
            return true;
        }
        return false;
    }
    public Order selectOrder(String id){
      return  mapper.selectOrder(id);
    }
    public List<Order> selectAllOrder(){
        return mapper.selectAllOrede();
    }
    public boolean takeorder(int fettlerid, String orderid){
        int status=0;
        status=mapper.updataFettlerId(fettlerid,orderid);
        if(status>0){
            return true;
        }
        return false;
    }
    public boolean completedorder(String orderid){
        int status=0;
        status=mapper.updataComplete(orderid);
        if(status>0){
            return true;
        }
        return false;
    }

    public String getOrdersLimited(int page, int type) {
        System.out.println(page+"---"+type);
        Order[] orders = mapper.getOrdersLimited(page, type);
        if(orders != null && orders.length > 0) {
            try {
                String json = new ObjectMapper().writeValueAsString(orders);
                return json;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 通过useid查询订单状态
     * @param userid
     * @return
     */
   public List<Order> selcetStatus(int userid,int status){return mapper.selectAllOredeByUserId(userid);}

   public String getOrderByStatus(int userid, int status) {
       Order[] orders = mapper.getOrdersByStatus(userid, status);
       String json = "";
       try {
           json = new ObjectMapper().writeValueAsString(orders);
       } catch (JsonProcessingException e) {
           e.printStackTrace();
       }
       return json;
   }
}
